/*
 * $Id: UiRequestSorter.java,v 1.4 2010/01/07 21:54:51 oliver Exp $
 * 
 * Copyright (C) 2009 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.ui.pojo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;

public class UiRequestSorter implements Comparator {
	Map<Long, List> base_map;

	public UiRequestSorter(Map<Long, List> base_map) {
		this.base_map = base_map;
	}

	@SuppressWarnings("unchecked")
	public UiRequestSorter(List<UiRequest> requests) {
		this.base_map = new HashMap<Long, List>();
		for (TimeSortableI request : requests) {
			//System.out.println("Time: " + request.getTime() + " request "+ request);
			if( !ASCUtil.isNullOrEmptyObject(request.getTime())){
				 if(base_map.containsKey(request.getTime())){
					base_map.get(request.getTime()).add(request);
				} else {
					List<TimeSortableI> timeList = new ArrayList<TimeSortableI>();
					timeList.add(request);
					base_map.put(request.getTime(), timeList);
				}
			} else {
				System.out.println("Skipping REquest with null start time");
			}
		}
	}

	public int compare(Object arg0, Object arg1) {
		if (!base_map.containsKey(arg0) || !base_map.containsKey(arg1)) {
			return 0;
		}
		if (((UiRequest) base_map.get(arg0)).getTime().longValue() < ((UiRequest) base_map
				.get(arg1)).getTime().longValue()) {
			return 1;
		} else if (((UiRequest) base_map.get(arg0)).getTime().longValue() == ((UiRequest) base_map
				.get(arg1)).getTime().longValue()) {
			return 0;
		} else {
			return -1;
		}
	}

	public Collection keySet() {
		return base_map.keySet();
	}

	public Map<Long, List> getMap() {
		return base_map;
	}
}
